package com.liveinstars.quhui.application.endpoint;

import com.liveinstars.api.dto.InterestDetailDto;
import com.liveinstars.api.dto.InterestDto;
import com.liveinstars.api.dto.InterestSimpleDto;
import com.liveinstars.api.dto.JoinInterest;
import com.liveinstars.api.facade.IInterestApi;
import com.liveinstars.api.query.InterestQuery;
import com.liveinstars.quhui.application.service.InterestApplicationService;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.QuHuiRuntimeException;
import com.onepiece.shipelves.common.vo.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class InterestEndpoint implements IInterestApi {


    private static final Logger logger = LoggerFactory.getLogger(InterestEndpoint.class);


    @Autowired
    InterestApplicationService interestApplicationService;


    @Override
    public Result<Page<InterestDto>> pageByQuery(@RequestBody InterestQuery query) {
        Result<Page<InterestDto>> result = new Result<>();
        try {
            Page<InterestDto> pageDos = interestApplicationService.pageByQuery(query);
            result.setData(pageDos);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<List<InterestSimpleDto>> getInterests() {
        Result<List<InterestSimpleDto>> result = new Result<>();
        try {
            result.setData(interestApplicationService.getInterests());
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<List<InterestDto>> getMyInterests(@RequestParam(value = "user_id") Integer userId) {
        Result<List<InterestDto>> result = new Result<>();
        try {
            result.setData(interestApplicationService.getMyInterests(userId));
        } catch (Exception e) {
            logger.error("getmyinterests", e);
            if (e instanceof QuHuiRuntimeException) {
                result = ((QuHuiRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<List<InterestSimpleDto>> getMyInterestsSimple(@RequestParam(value = "user_id") Integer userId) {
        Result<List<InterestSimpleDto>> result = new Result<>();
        try {
            result.setData(interestApplicationService.getMyInterestsSimple(userId));
        } catch (Exception e) {
            if (e instanceof QuHuiRuntimeException) {
                result = ((QuHuiRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<InterestDetailDto> getDetailById(@RequestParam(value = "id") Integer id) {
        Result<InterestDetailDto> result = new Result<>();
        try{
            result.setData(interestApplicationService.getDetailById(id));
        }catch (Exception e) {
            if (e instanceof QuHuiRuntimeException) {
                result = ((QuHuiRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<Boolean> joinInterest(@RequestBody JoinInterest dto, @RequestHeader("uid") Integer userId) {
        Result<Boolean> result = new Result<>();
        try{
            result.setData(interestApplicationService.joinInterest(dto, userId));
        }catch (Exception e) {
            logger.error("joininterest", e);
            if (e instanceof QuHuiRuntimeException) {
                result = ((QuHuiRuntimeException) e).createResult();
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }


}
